/*
 * TabAdd.cpp
 *
 *  Created on: 9 déc. 2013
 *      Author: Raphaël
 */

#include <iostream>
#include "raphTools/SystemTools.h"
#include "TabAdd.h"
#include "AddThread.h"

using namespace raph;
using namespace raph::type;

std::shared_ptr<T_FLOAT128> TabAdd::add(const std::vector<T_FLOAT128>& vecA,
        const std::vector<T_FLOAT128>& vecB)
    {
    std::shared_ptr<T_FLOAT128> ptrTabResult(new T_FLOAT128[vecA.size()], [](T_FLOAT128 *p)
        {
        std::cout<<"the tab is going to delete"<<std::endl;
        delete[] p;
        });
    T_UINT64 N = SystemTools::getNbCores();
    std::vector<std::shared_ptr<Thread>> vecThreads;
    vecThreads.reserve(N);
    for (T_UINT64 position = 0; position < N; ++position)
        {
        vecThreads.push_back(
                std::shared_ptr<Thread>(new AddThread(vecA, vecB, ptrTabResult, N, position)));
        vecThreads.back().get()->start();
        }

    try
        {
        doJoin(vecThreads);
        }
    catch (std::exception &e)
        {
        std::cerr << e.what() << std::endl;
        }

    return ptrTabResult;
    }

void TabAdd::doJoin(const std::vector<std::shared_ptr<Thread> >& vecThreads)
    {
    try
        {
        for (auto ptr : vecThreads)
            {
            ptr.get()->join();
            }
        }
    catch (std::exception &e)
        {
        throw;
        }
    }

void TabAdd::toto()
    {
    std::cout << "hahahaahahah" << std::endl;
    }

